<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ESP32Cam</title>
</head>
<body>
<img src="" class="img" id="STREAM">
<h3 id="text"></h3>
<hr/>
<button id="switchBtn" onclick="closeWebSocket()">关闭连接</button>
<hr/>
<div id="message"></div>
</body>
<!--<script src="/src/jquery.min.js"></script>-->
<script type="text/javascript">
    var server = 'ws://192.168.137.80:86';
    server = server.replace('http://', '').replace('/:', ':')
    if ('WebSocket' in window) {
        var socket = new WebSocket(server);
    } else {
        alert('当前浏览器 Not support websocket')
    }
    socket.binaryType = 'arraybuffer';
    if (socket) {
        socket.onopen = function () {
        }
        socket.onmessage = async function (msg) {
            if (msg.data.length !== undefined) {
                console.log("zfc");
                document.getElementById('text').innerHTML = msg.data;
                return;
            }
            var bytes = new Uint8Array(msg.data);
            var binary = '';
            var len = bytes.byteLength;
            for (var i = 0; i < len; i++) {
                binary += String.fromCharCode(bytes[i])
            }
            var img = document.getElementById('STREAM');
            img.src = 'data:image/jpg;base64,' + window.btoa(binary);
        }
        socket.onclose = function () {
            showServerResponse('The connection has been closed.');
        }
//连接发生错误的回调方法
        socket.onerror = function () {
            setMessageInnerHTML("WebSocket连接发生错误");
        };

//连接成功建立的回调方法
        socket.onopen = function () {
            setMessageInnerHTML("WebSocket连接成功");
        }

//连接关闭的回调方法
        socket.onclose = function () {
            setMessageInnerHTML("WebSocket连接关闭");
        }
        //发送消息
        //关闭WebSocket连接

        //将消息显示在网页上
        function setMessageInnerHTML(innerHTML) {
            document.getElementById('message').innerHTML += innerHTML + '<br/>';
        }
//关闭WebSocket连接
        function closeWebSocket() {
            socket.close();
        }

        //发送消息
        function send() {
            var message = document.getElementById('text').value;
            socket.send(message);
        }

        var webState = true;


    }
</script>
</html>
